home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / share / pyshared / parted / constraint.py < prev    next >
Encoding:
Python Source  |  2010-06-29  |  7.7 KB  |  172 lines

  1. #
  2. # constraint.py
  3. # Python bindings for libparted (built on top of the _ped Python module).
  4. #
  5. # Copyright (C) 2009 Red Hat, Inc.
  6. #
  7. # This copyrighted material is made available to anyone wishing to use,
  8. # modify, copy, or redistribute it subject to the terms and conditions of
  9. # the GNU General Public License v.2, or (at your option) any later version.
  10. # This program is distributed in the hope that it will be useful, but WITHOUT
  11. # ANY WARRANTY expressed or implied, including the implied warranties of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
  13. # Public License for more details.  You should have received a copy of the
  14. # GNU General Public License along with this program; if not, write to the
  15. # Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. # 02110-1301, USA.  Any Red Hat trademarks that are incorporated in the
  17. # source code or documentation are not subject to the GNU General Public
  18. # License and may only be used or replicated with the express permission of
  19. # Red Hat, Inc.
  20. #
  21. # Red Hat Author(s): Chris Lumens <clumens@redhat.com>
  22. #                    David Cantrell <dcantrell@redhat.com>
  23. #
  24.  
  25. import parted
  26. import _ped
  27.  
  28. from decorators import localeC
  29.  
  30. class Constraint(object):
  31.     """Constraint()
  32.  
  33.        A Constraint object describes a set of restrictions on other pyparted
  34.        operations.  Constraints can restrict the location and alignment of the
  35.        start and end of a partition, and its minimum and maximum size.  Most
  36.        constraint operations can raise CreateException if creating temporary
  37.        objects fails, or ArithmeticError if an error occurs during
  38.        calculations."""
  39.     @localeC
  40.     def __init__(self, *args, **kwargs):
  41.         """Create a new Constraint object.  There are many different ways to
  42.            create a Constraint, all depending on the parameters passed to
  43.            __init__.  If minGeom and maxGeom are supplied, the constraint will
  44.            be created to satisfy both.  If only one of minGeom or maxGeom are
  45.            supplied, the constraint is only guaranteed to solve the given
  46.            paramter.  If exactGeom is given, the constraint will only be
  47.            satisfied by the given geometry.  If device is given, any region
  48.            on that device will satisfy the constraint.
  49.  
  50.            If none of the previously mentioned parameters are supplied, all of
  51.            startAlign, EndAlign, startRange, endRange, minSize, and maxSize
  52.            must be given."""
  53.         if kwargs.has_key("PedConstraint"):
  54.             self.__constraint = kwargs.get("PedConstraint")
  55.         elif kwargs.has_key("minGeom") and kwargs.has_key("maxGeom"):
  56.             ming = kwargs.get("minGeom").getPedGeometry()
  57.             maxg = kwargs.get("maxGeom").getPedGeometry()
  58.             self.__constraint = _ped.constraint_new_from_min_max(ming, maxg)
  59.         elif kwargs.has_key("minGeom"):
  60.             ming = kwargs.get("minGeom").getPedGeometry()
  61.             self.__constraint = _ped.constraint_new_from_min(ming)
  62.         elif kwargs.has_key("maxGeom"):
  63.             maxg = kwargs.get("maxGeom").getPedGeometry()
  64.             self.__constraint = _ped.constraint_new_from_max(maxg)
  65.         elif kwargs.has_key("exactGeom"):
  66.             exact = kwargs.get("exactGeom").getPedGeometry()
  67.             self.__constraint = _ped.constraint_exact(exact)
  68.         elif kwargs.has_key("device"):
  69.             dev = kwargs.get("device").getPedDevice()
  70.             self.__constraint = _ped.constraint_any(dev)
  71.         elif kwargs.has_key("startAlign") and kwargs.has_key("endAlign") and \
  72.              kwargs.has_key("startRange") and kwargs.has_key("endRange") and \
  73.              kwargs.has_key("minSize") and kwargs.has_key("maxSize"):
  74.             starta = kwargs.get("startAlign").getPedAlignment()
  75.             enda = kwargs.get("endAlign").getPedAlignment()
  76.             startr = kwargs.get("startRange").getPedGeometry()
  77.             endr = kwargs.get("endRange").getPedGeometry()
  78.             mins = kwargs.get("minSize")
  79.             maxs = kwargs.get("maxSize")
  80.             self.__constraint = _ped.Constraint(starta, enda, startr, endr,
  81.                                                 mins, maxs)
  82.         else:
  83.             raise parted.ConstraintException, "missing initialization parameters"
  84.  
  85.     def __eq__(self, other):
  86.         return not self.__ne__(other)
  87.  
  88.     def __ne__(self, other):
  89.         if hash(self) == hash(other):
  90.             return False
  91.  
  92.         if type(self) != type(other):
  93.             return True
  94.  
  95.         c1 = self.getPedConstraint()
  96.         c2 = other.getPedConstraint()
  97.  
  98.         return self.minSize != other.minSize \
  99.                 or self.maxSize != other.maxSize \
  100.                 or c1.start_align != c2.start_align \
  101.                 or c1.end_align != c2.end_align \
  102.                 or c1.start_range != c2.start_range \
  103.                 or c1.end_range != c2.end_range
  104.  
  105.     startAlign = property(
  106.             lambda s: parted.Alignment(PedAlignment=s.__constraint.start_align),
  107.             lambda s, v: setattr(s.__constraint, "start_align", v.getPedAlignment()))
  108.  
  109.     endAlign = property(
  110.             lambda s: parted.Alignment(PedAlignment=s.__constraint.end_align),
  111.             lambda s, v: setattr(s.__constraint, "end_align", v.getPedAlignment()))
  112.  
  113.     startRange = property(
  114.             lambda s: parted.Geometry(PedGeometry=s.__constraint.start_range),
  115.             lambda s, v: setattr(s.__constraint, "start_range", v.getPedGeometry()))
  116.  
  117.     endRange = property(
  118.             lambda s: parted.Geometry(PedGeometry=s.__constraint.end_range),
  119.             lambda s, v: setattr(s.__constraint, "end_range", v.getPedGeometry()))
  120.  
  121.     minSize = property(
  122.             lambda s: s.__constraint.min_size,
  123.             lambda s, v: setattr(s.__constraint, "min_size", v))
  124.  
  125.     maxSize = property(
  126.             lambda s: s.__constraint.max_size,
  127.             lambda s, v: setattr(s.__constraint, "max_size", v))
  128.  
  129.     def __str__(self):
  130.         s = ("parted.Constraint instance --\n"
  131.              "  startAlign: %(startAlign)r  endAlign: %(endAlign)r\n"
  132.              "  startRange: %(startRange)r  endRange: %(endRange)r\n"
  133.              "  minSize: %(minSize)s  maxSize: %(maxSize)s\n"
  134.              "  PedConstraint: %(ped)r" %
  135.              {"startAlign": self.startAlign, "endAlign": self.endAlign,
  136.               "startRange": self.startRange, "endRange": self.endRange,
  137.               "minSize": self.minSize, "maxSize": self.maxSize,
  138.               "ped": self.__constraint})
  139.         return s
  140.  
  141.     @localeC
  142.     def intersect(self, b):
  143.         """Return a new constraint that is the intersection of self and the
  144.            provided constraint b.  The returned constraint will therefore be
  145.            more restrictive than either input as it will have to satisfy
  146.            both."""
  147.         return parted.Constraint(PedConstraint=self.__constraint.intersect(b.getPedConstraint()))
  148.  
  149.     @localeC
  150.     def solveMax(self):
  151.         """Return a new geometry that is the largest region satisfying self.
  152.            There may be more than one solution, and there are no guarantees as
  153.            to which solution will be returned."""
  154.         return parted.Geometry(PedGeometry=self.__constraint.solve_max())
  155.  
  156.     @localeC
  157.     def solveNearest(self, geom):
  158.         """Return a new geometry that is the nearest region to geom that
  159.            satisfies self.  This function does not guarantee any specific
  160.            meaning of 'nearest'."""
  161.         return parted.Geometry(PedGeometry=self.__constraint.solve_nearest(geom.getPedGeometry()))
  162.  
  163.     @localeC
  164.     def isSolution(self, geom):
  165.         """Does geom satisfy this constraint?"""
  166.         return self.__constraint.is_solution(geom.getPedGeometry())
  167.  
  168.     def getPedConstraint(self):
  169.         """Return the _ped.Constraint object contained in this Constraint.
  170.            For internal module use only."""
  171.         return self.__constraint
  172.